package br.com.manutencaocarro.helper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.Spinner;
import android.widget.TextView;
import br.com.manutencaocarro.R;
import br.com.manutencaocarro.activity.CadastraPecaActivity;
import br.com.manutencaocarro.dao.CarroDAO;
import br.com.manutencaocarro.dao.LojaDAO;
import br.com.manutencaocarro.modelo.Carro;
import br.com.manutencaocarro.modelo.Loja;
import br.com.manutencaocarro.modelo.Peca;

public class CadastroPecaHelper 
{
	private TextView nomePeca;
	private TextView qtdPecas;
	private TextView valorPeca;
	private Spinner carros;
	private Spinner lojas;
	private Spinner units;
	private DatePicker dataCompraPeca;
	private int layout = android.R.layout.simple_spinner_item;
	
	public CadastroPecaHelper(CadastraPecaActivity cadastraPecaActivity) 
	{
		nomePeca = (TextView) cadastraPecaActivity.findViewById(R.id.nomePeca);
		qtdPecas = (TextView) cadastraPecaActivity.findViewById(R.id.qtdPecas);
		valorPeca = (TextView) cadastraPecaActivity.findViewById(R.id.valorPeca);
		
		
		List<String> unidades = preencheUnidades();
		ArrayAdapter<String> adapterPeca = new ArrayAdapter<String>( cadastraPecaActivity, layout, unidades );
		units = (Spinner) cadastraPecaActivity.findViewById(R.id.units);
		units.setAdapter(adapterPeca);
		
		CarroDAO dao = new CarroDAO(cadastraPecaActivity);
		List<Carro> listaCarros = dao.getLista();
		dao.close();
		
		ArrayAdapter<Carro> adapterCarro = new ArrayAdapter<>(cadastraPecaActivity, layout , listaCarros );
		carros = (Spinner) cadastraPecaActivity.findViewById(R.id.lista_carros);
		carros.setAdapter(adapterCarro);
		
		LojaDAO ljDao = new LojaDAO(cadastraPecaActivity);
		List<Loja> listaLojas = ljDao.getLista();
		ljDao.close();
		
		ArrayAdapter<Loja> adapterLoja = new ArrayAdapter<>( cadastraPecaActivity, layout , listaLojas );
		lojas = (Spinner) cadastraPecaActivity.findViewById(R.id.lista_lojas);
		lojas.setAdapter(adapterLoja);
		
		dataCompraPeca = (DatePicker) cadastraPecaActivity.findViewById(R.id.dataCompraPeca);
	}

	private List<String> preencheUnidades() 
	{
		List<String> unidades = new ArrayList<>();
		unidades.add("Unidade");
		unidades.add("Pacote");
		
		return unidades;
	}

	@SuppressLint("SimpleDateFormat")
	public Peca pegaPecaCadastro()
	{
		Peca peca = new Peca();
		peca.setNomePeca(nomePeca.getText().toString());
		
		if( !qtdPecas.getText().toString().equals( "" ) )
			peca.setQtdPecas(Long.valueOf(qtdPecas.getText().toString()));
		else
			peca.setQtdPecas(0l);
		
		peca.setUnidade((int)units.getSelectedItemId());
		
		if( !valorPeca.getText().toString().equals( "" ) )
			peca.setValorPeca(Float.valueOf(valorPeca.getText().toString()));
		else
			peca.setValorPeca(Float.valueOf(valorPeca.getText().toString()));
		
		Carro carro = (Carro) carros.getSelectedItem();
		
		peca.setIdCarro(carro.getId());
		
		Loja loja = (Loja) lojas.getSelectedItem();
		
		peca.setIdLoja(loja.getId_loja());
		
		String date = new SimpleDateFormat("yyyy-MM-dd").format(dataCompraPeca.getCalendarView().getDate());
		GregorianCalendar calendar;
		String day = null;
		String month = null;
		String year = null;
		
		if( date.matches("\\d{4}-\\d{2}-\\d{2}") )
		{
			day = date.split( "-" )[2];
			month = date.split( "-" )[1];
			year = date.split( "-" )[0];
		}
		
		calendar = new GregorianCalendar(Integer.valueOf(year), Integer.valueOf(month), Integer.valueOf(day));
		calendar.add( GregorianCalendar.MONTH, -1 );
		peca.setDataCompra( calendar.getTime() );
		
		return peca;
	}

	@SuppressLint("DefaultLocale")
	public void colocaPecaNoFormulario( CadastraPecaActivity cadastraPecaActivity, Peca pecaSelecionada ) 
	{
		HashMap<String, String> months = new HashMap<>();
		months.put( "JAN", "1" );
		months.put( "FEV", "2" );
		months.put( "FEB", "2" );
		months.put( "MAR", "3" );
		months.put( "ABR", "4" );
		months.put( "APR", "4" );
		months.put( "MAI", "5" );
		months.put( "MAY", "5" );
		months.put( "JUN", "6" );
		months.put( "JUL", "7" );
		months.put( "AGO", "8" );
		months.put( "AUG", "8" );
		months.put( "SET", "9" );
		months.put( "SEP", "9" );
		months.put( "OUT", "10" );
		months.put( "OCT", "10" );
		months.put( "NOV", "11" );
		months.put( "DEZ", "12" );
		months.put( "DEC", "12" );
		
		nomePeca.setText( pecaSelecionada.getNomePeca() );
		qtdPecas.setText( pecaSelecionada.getQtdPecas().toString() );
		valorPeca.setText( String.valueOf( pecaSelecionada.getValorPeca() ) );
		
		CarroDAO dao = new CarroDAO(cadastraPecaActivity);
		List<Carro> listaCarros = dao.getCarro( pecaSelecionada.getIdCarro() );
		dao.close();
		
		ArrayAdapter<Carro> adapterCarro = new ArrayAdapter<>(cadastraPecaActivity, layout , listaCarros );
		carros = (Spinner) cadastraPecaActivity.findViewById(R.id.lista_carros);
		carros.setAdapter(adapterCarro);
		
		LojaDAO lojaDao = new LojaDAO(cadastraPecaActivity);
		List<Loja> listaLojas = lojaDao.getLoja( pecaSelecionada.getIdLoja() );
		lojaDao.close();
		
		ArrayAdapter<Loja> adapterLoja = new ArrayAdapter<>(cadastraPecaActivity, layout , listaLojas );
		lojas = (Spinner) cadastraPecaActivity.findViewById(R.id.lista_lojas);
		lojas.setAdapter(adapterLoja);
		
		String date = pecaSelecionada.getDataCompra().toString();
		
		String day;
		String month;
		String year;
		
		if( date.matches("\\d{4}-\\d{2}-\\d{2}") )
		{
			day = date.split( "-" )[2];
			month = date.split( "-" )[1];
			year = date.split( "-" )[0];
		} else{
			day = date.split( " " )[2];
			month = date.split( " " )[1];
			year = date.split( " " )[5];
		}
		
		if( !month.equals(null) && !month.matches("\\d{2}") )
			month = months.get(month.toUpperCase());
		
		dataCompraPeca.updateDate( Integer.valueOf(year), Integer.valueOf(month) - 1, Integer.valueOf(day) );
	}
	
	public void setReadOnly()
	{
		nomePeca.setEnabled(false);
		qtdPecas.setEnabled(false);
		valorPeca.setEnabled(false);
		carros.setEnabled(false);
		lojas.setEnabled(false);
		units.setEnabled(false);
		dataCompraPeca.setEnabled(false);
	}
}